//
// Created by wzr on 2021/5/22.
//
#include "data.h"

void executeStatement(const string& query)
{
    mysql::MySQL_Driver *driver;
    Connection *con;
    driver = mysql::get_mysql_driver_instance();
    con = driver->connect(MYSQLURL, ACCOUNT, PWD);
    Statement *stmt;
    stmt = con->createStatement();
    stmt->execute("USE cpp");
    stmt->execute(query);
}

/*
 * table    表名
 * column   要更改的字段名
 * delta    改多少
 * where    定位字段名
 * id       字段名对应值 一般为id
 */
void executeQueryStatement(const string& table, const string& column, int delta, const string& where, int id)
{
    /*
     * 1. Create a connection
     */
    mysql::MySQL_Driver     *driver;
    Connection              *con;
    driver = mysql::get_mysql_driver_instance();
    con = driver->connect(MYSQLURL, ACCOUNT, PWD);
    Statement *stmt;
    stmt = con->createStatement();
    stmt->execute("USE cpp");

    /*
     * 2. Create statement
     */
    ostringstream ostr;
    ostr << "UPDATE " << table << " SET "
         << column << "=" << column << "+" << delta << " WHERE " << where << "=" << id;
    string query = ostr.str();
    stmt->execute(query);
}

void incTaking(int num)
{
    string query = "UPDATE Cards SET taking=taking+1 WHERE Num="+ to_string(num);
    executeStatement(query);
}

void updateBalance(int num, int delta)
{
    string table = "Cards";
    string column = "Balance";
    string where = "Num";
    executeQueryStatement(table, column, delta, where, num);
}

void updateFree(int num, int delta)
{
    string table = "Free";
    string column = "Balance";
    string where = "Num";
    executeQueryStatement(table, column, delta, where, num);
}

User randomUser()
{
    mysql::MySQL_Driver *drive;
    Connection *conn;
    drive = mysql::get_mysql_driver_instance();
    conn = drive->connect(MYSQLURL, ACCOUNT, PWD);
    conn->setSchema("cpp");
    Statement* stmt;
    stmt = conn->createStatement();
    sql::ResultSet *res1 = stmt->executeQuery("SELECT max(Id) AS count from Users");
    res1->next();
    int count = res1->getInt("count");
    int UserId = rand()%count+1;
    sql::ResultSet *res2 = stmt->executeQuery("SELECT * from Users where Id="+ to_string(UserId));
    res2->next();
    int id = res2->getInt("Id");
    int type = res2->getInt("type");
    string name = res2->getString("name");
    string position = res2->getString("position");
    User user = User(id, type, name, position);
    return user;
}

bool getCard(User *user, Card *&card1)
{
    mysql::MySQL_Driver *drive;
    Connection *conn;
    drive = mysql::get_mysql_driver_instance();
    conn = drive->connect(MYSQLURL, ACCOUNT, PWD);
    conn->setSchema("cpp");
    Statement* stmt;
    stmt = conn->createStatement();
    sql::ResultSet *res3 = stmt->executeQuery("SELECT * from Cards where UserId="+ to_string(user->getId()));
    if(!res3->next())
    {
        return false;
    }else
    {
        int no = res3->getInt("Num");
        int balance = res3->getInt("Balance");
        int typ = res3->getInt("Type");
        int taking = res3->getInt("taking");
        int free = res3->getInt("free");
        int state = res3->getInt("state");
        if (typ == 1) {
            Card *card = new TeacherCard(user, no, balance, taking, state);
            card1 = card;
        } else if (typ == 2) {
            Card *card = new StudentCard(user, no, balance, taking, state);
            card1 = card;
        } else {
            Card *card = new LimitCard(user, no, balance, taking, free, state);
            card1 = card;
        }
        return true;
    }
}

Bus getBus()
{
    mysql::MySQL_Driver *drive;
    Connection *conn;
    drive = mysql::get_mysql_driver_instance();
    conn = drive->connect(MYSQLURL, ACCOUNT, PWD);
    conn->setSchema("cpp");
    Statement* stmt;
    stmt = conn->createStatement();
    sql::ResultSet *res1 = stmt->executeQuery("SELECT max(BusId) AS count from Buses");
    res1->next();
    int count = res1->getInt("count");
    int BusId = rand()%count+1;
    sql::ResultSet *res2 = stmt->executeQuery("SELECT * from Buses where BusId="+ to_string(BusId));
    res2->next();
    string license  =   res2->getString("License");
    string model    =   res2->getString("Model");
    string driver   =   res2->getString("Driver");
    int    carring  =   res2->getInt("Carring");
    int    running  =   res2->getInt("RunningTime");
    int    restTime =   res2->getInt("RestTime");
    Bus bus = Bus(license, model, driver, carring, Date(2021, 5,22), Period(Time(7, 0), Time(21,00)), running, restTime);
    return bus;
}